package com.airnewslinkosky.ticket.ui.personcenter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import com.airnewslinkosky.ticket.R;
import com.airnewslinkosky.ticket.model.ItineraryInfo;
import com.airnewslinkosky.ticket.pay.util.BaseHelper;
import com.airnewslinkosky.ticket.sql.FavoriteInfoDB;
import com.airnewslinkosky.ticket.ui.BaseActivity;
import com.airnewslinkosky.ticket.ui.PersonCenter;
import com.mobclick.android.MobclickAgent;

/**
 * 收件人新增
 * @author luoxz
 *
 */
public class PersonItineraryInfoAdd extends BaseActivity implements
		AdapterView.OnItemClickListener {

	public  final int DIALOG_SINGLE_CHOICE = 9;
	private Button backBut;
	
	public static List<Map<String,String>> passengerList = new ArrayList<Map<String,String>>();
	public static List<Map<String,String>> contactList = new ArrayList<Map<String,String>>();
	public static List<Map<String,String>> itineraryList = new ArrayList<Map<String,String>>();
	public static List<Map<String,String>> mileagerInfoList = new ArrayList<Map<String,String>>();
	
	public SimpleAdapter sa;
	public ListView lv;
	public Button saveBut;
	public List<Map<String, String>> data = new ArrayList<Map<String, String>>();

	private ProgressDialog progressDialog = null;
    
    //收件人姓名,收件人电话,收件地址,收件邮编
    private EditText consigneeName,consigneePhone,consigneeAddress,consigneeZIP;
    private Toast toast;

	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.fly_itinerary_info);
		// 删除窗口背景
        getWindow().setBackgroundDrawable(null);
		activityList.add(this);
		

		consigneeName = (EditText)findViewById(R.id.consigneeName);
		consigneePhone = (EditText)findViewById(R.id.consigneePhone);
		consigneeAddress = (EditText)findViewById(R.id.consigneeAddress);
		consigneeZIP = (EditText)findViewById(R.id.consigneeZIP);
        
		saveBut = (Button)findViewById(R.id.saveBut);
		
		backBut = (Button)findViewById(R.id.backBut);
		
		OnClickListener b_ocl= new OnClickListener() { 
	    	public void onClick(View v) { 
	    		switch (v.getId()) {
		    		case R.id.saveBut:
		    			if ("".equals(consigneeName.getText().toString()) || "".equals(consigneePhone.getText().toString()) || "".equals(consigneeAddress.getText().toString()) || "".equals(consigneeZIP.getText().toString())){
//		    				Toast.makeText(PersonItineraryInfoAdd.this, "请填写完整信息", Toast.LENGTH_LONG).show();
		    				BaseHelper.showDialog(PersonItineraryInfoAdd.this, "提示", "请填写完整信息",
		    						R.drawable.infoicon);
		    				return;
		    			}
		    			String name = consigneeName.getText().toString();
		    			
						if(!name.matches("(^[\u4e00-\u9fa5]{2,6}$)|(^[a-zA-z][a-zA-z\\s]{0,18}[a-zA-z]$)")){
		    				toast = Toast.makeText(PersonItineraryInfoAdd.this, "姓名应为2-6个中文字符或2-20个英文", Toast.LENGTH_LONG);
		    				toast.setGravity(Gravity.CENTER, 0, 0);
		    				toast.show();
		    				return;				
		    			}
						if (!consigneePhone.getText().toString().matches("[0-9]{11}")) {
							toast = Toast.makeText(PersonItineraryInfoAdd.this, "手机号填写不正确", Toast.LENGTH_LONG);
		    				toast.setGravity(Gravity.CENTER, 0, 0);
		    				toast.show();
							return;
						}
						if (consigneeZIP.getText().toString().length() != 6) {
							toast = Toast.makeText(PersonItineraryInfoAdd.this, "邮编应该为六位数字", Toast.LENGTH_LONG);
		    				toast.setGravity(Gravity.CENTER, 0, 0);
		    				toast.show();
							return;
						}
		    			HashMap<String, String> map = new HashMap<String, String>();
		    			map.put("consigneeName", consigneeName.getText().toString());
		    			map.put("consigneePhone", consigneePhone.getText().toString());
		    			map.put("consigneeAddress", consigneeAddress.getText().toString());
		    			map.put("consigneeZIP", consigneeZIP.getText().toString());
		    			
		    			data.add(map);

		    			for (int i = 0; i < data.size(); i++) {
		    				Map<String,String> m = data.get(i);
		    				ItineraryInfo itineraryInfo = new ItineraryInfo();
		    				itineraryInfo.setConsigneeName(m.get("consigneeName"));
		    				itineraryInfo.setConsigneePhone(m.get("consigneePhone"));
		    				itineraryInfo.setConsigneeAddress(m.get("consigneeAddress"));
		    				itineraryInfo.setConsigneeZIP(m.get("consigneeZIP"));
		    				FavoriteInfoDB.setItineraryInfo(PersonItineraryInfoAdd.this, itineraryInfo);
		    				break;
		    			}
		    			
		    			Intent intent = new Intent();
		    			intent.setClass(PersonItineraryInfoAdd.this,PersonItineraryInfoList.class);
		    			startActivity(intent);
		    			
		    			break;
		    		case R.id.backBut:
		    			Intent intent3 = new Intent();
		    			intent3.setClass(PersonItineraryInfoAdd.this,PersonCenter.class);
		    			startActivity(intent3);
		    			break;
	    		}
	    	}
		};
		
		backBut.setOnClickListener(b_ocl);
		saveBut.setOnClickListener(b_ocl);
	    
	}
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		// TODO Auto-generated method stub
		
	}
	
	public void onResume() {     
		super.onResume();     
		MobclickAgent.onResume(this); 
	}  
	
	public void onPause() {     
		super.onPause();     
		MobclickAgent.onPause(this); 
	}  
	
}
